from collections import OrderedDict

import torch
import torch.nn as nn
from torch import Tensor

from utils.conv.BasicConv2d import BasicConv2d


class BasicBlock(nn.Module):
    def __init__(self, in_channels, features):
        super().__init__()

        self.conv1 = nn.Conv2d(
            in_channels=in_channels,
            out_channels=features,
            kernel_size=3,
            padding=1,
            bias=False,
        )
        self.norm1 = nn.BatchNorm2d(num_features=features)
        self.relu1 = nn.ReLU(inplace=True)
        self.conv2 = nn.Conv2d(
            in_channels=features,
            out_channels=features,
            kernel_size=3,
            padding=1,
            bias=False,
        )
        self.norm2 = nn.BatchNorm2d(num_features=features)
        self.relu2 = nn.ReLU(inplace=True)

    def forward(self, x: Tensor) -> Tensor:
        x = self.conv1(x)
        x = self.norm1(x)
        x = self.relu1(x)
        identity = x

        x = self.conv2(x)
        x = self.norm2(x)
        x += identity
        x = self.relu2(x)

        return x


class BasicGBlock(nn.Module):
    def __init__(self, in_channels, features):
        super().__init__()

        self.conv1_a = BasicConv2d(
            in_channels=in_channels,
            out_channels=features,
            kernel_size=(1, 7),
            padding=(0, 3),
        )
        self.conv1_b = BasicConv2d(
            # in_channels=features,
            in_channels=in_channels,
            out_channels=features,
            kernel_size=(7, 1),
            padding=(3, 0),
        )
        self.conv1_c = BasicConv2d(
            in_channels=features * 2,
            out_channels=features,
            kernel_size=1,
        )
        self.conv2_a = BasicConv2d(
            in_channels=features,
            out_channels=features,
            kernel_size=(1, 7),
            padding=(0, 3),
        )
        self.conv2_b = BasicConv2d(
            in_channels=features,
            out_channels=features,
            kernel_size=(7, 1),
            padding=(3, 0),
        )
        self.conv2_c = BasicConv2d(
            in_channels=features * 3 + in_channels,
            out_channels=features,
            kernel_size=1,
        )

    def forward(self, x: Tensor) -> Tensor:
        identity = x
        branch = [self.conv1_a(x), self.conv1_b(x)]
        branch = torch.cat(branch, dim=1)
        x = self.conv1_c(branch)
        branch = [self.conv2_a(x), self.conv2_b(x), identity, x]
        branch = torch.cat(branch, dim=1)
        x = self.conv2_c(branch)

        return x


class simUNet(nn.Module):

    def __init__(self, in_channels=3, out_channels=1, init_features=32):
        super(simUNet, self).__init__()

        features = init_features
        self.encoder1 = BasicBlock(in_channels, features)
        self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2)
        self.encoder2 = BasicBlock(features, features * 2)
        self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2)
        self.encoder3 = BasicBlock(features * 2, features * 4)
        self.pool3 = nn.MaxPool2d(kernel_size=2, stride=2)
        self.encoder4 = BasicBlock(features * 4, features * 8)
        self.pool4 = nn.MaxPool2d(kernel_size=2, stride=2)

        self.bottleneck = BasicGBlock(features * 8, features * 16)

        self.upconv4 = nn.ConvTranspose2d(
            features * 16, features * 8, kernel_size=2, stride=2
        )
        self.decoder4 = BasicBlock((features * 8) * 2, features * 8)
        self.upconv3 = nn.ConvTranspose2d(
            features * 8, features * 4, kernel_size=2, stride=2
        )
        self.decoder3 = BasicBlock((features * 4) * 2, features * 4)
        self.upconv2 = nn.ConvTranspose2d(
            features * 4, features * 2, kernel_size=2, stride=2
        )
        self.decoder2 = BasicBlock((features * 2) * 2, features * 2)
        self.upconv1 = nn.ConvTranspose2d(
            features * 2, features, kernel_size=2, stride=2
        )
        self.decoder1 = BasicBlock(features * 2, features)

        self.conv = nn.Conv2d(
            in_channels=features, out_channels=out_channels, kernel_size=1
        )

    def forward(self, x):
        enc1 = self.encoder1(x)
        enc2 = self.encoder2(self.pool1(enc1))
        enc3 = self.encoder3(self.pool2(enc2))
        enc4 = self.encoder4(self.pool3(enc3))

        bottleneck = self.bottleneck(self.pool4(enc4))

        dec4 = self.upconv4(bottleneck)
        dec4 = torch.cat((dec4, enc4), dim=1)
        dec4 = self.decoder4(dec4)
        dec3 = self.upconv3(dec4)
        dec3 = torch.cat((dec3, enc3), dim=1)
        dec3 = self.decoder3(dec3)
        dec2 = self.upconv2(dec3)
        dec2 = torch.cat((dec2, enc2), dim=1)
        dec2 = self.decoder2(dec2)
        dec1 = self.upconv1(dec2)
        dec1 = torch.cat((dec1, enc1), dim=1)
        dec1 = self.decoder1(dec1)
        return torch.sigmoid(self.conv(dec1))
